package com.sgl.blog.controller.blog;

import com.github.pagehelper.PageInfo;
import com.sgl.blog.pojo.*;
import com.sgl.blog.service.ArticleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 简介
 * 作者： 李志伟
 * 日期： 2020/4/3
 * 时间： 20:34
 */
@Controller
@RequestMapping("blog")
public class ArticleController {

    @Resource
    private ArticleService articleService;


    /**
     * @return 转发到详情页
     */
    @GetMapping("details/{id}")
    public String details(@PathVariable(name = "id") Integer id, Model model){
        Map<String, Object> article = articleService.findBlogById(id);
        if (article != null){
            model.addAttribute("blogArticle",article);
            return "blog/details";
        }
        return "error/404";
    }

    /**
     * @return 携带数据转发到我的博客
     */
    @GetMapping("myBlog")
    public String myBlog(HttpSession session,Model model){
        BlogAccount account = (BlogAccount) session.getAttribute("account");
        if (account != null){
            model.addAttribute("myBlogList",articleService.findBlogByAccount(account.getAccountId()));
        }
        return "blog/myBlog";
    }

    /**
     * 删除
     * @param article 删除的文章
     * @param session 权限
     */
    @DeleteMapping("myBlog")
    public @ResponseBody Restful<PageInfo<BlogArticle>> deleteBlog(@RequestBody BlogArticle article , HttpSession session){
        BlogAccount account = (BlogAccount) session.getAttribute("account");
        if (account != null){
            article.setAccountId(account.getAccountId());
            return articleService.removeBlog(article) ? RestfulBuilder.successNoData(ResultCode.SUCCESS) : RestfulBuilder.faile(ResultCode.PARAM_NOT_COMPLETE);
        }
        return RestfulBuilder.faile(ResultCode.USER_NOT_LOGGED_IN);
    }

    /**
     * @return 分页查询
     */
    @GetMapping("page/{page}")
    public @ResponseBody Restful<PageInfo<BlogArticle>> list(@PathVariable(name = "page") Integer page) {
        PageInfo<BlogArticle> blogPage = articleService.findBlogByPage(page);
        return RestfulBuilder.success(blogPage, ResultCode.SUCCESS);
    }

    /**
     * 添加博客
     * @param article 博客信息
     * @param session 获得session域
     * @return 返回到markdown编辑器页面
     */
    @PostMapping("article")
    public String articleAdd(BlogArticle article, HttpSession session) {
        BlogAccount account = (BlogAccount) session.getAttribute("account");
        article.setAccountId(account.getAccountId());
        if (article.getArticleState() == null) {
            article.setArticleState(1);
        }
        articleService.insertBlog(article);
        return "markdown";
    }

    @PostMapping("articleSave")
    @ResponseBody
    public Restful articleSave(@RequestBody BlogArticle article, HttpSession session) {
        BlogAccount account = (BlogAccount) session.getAttribute("account");
        if (account != null){
            article.setAccountId(account.getAccountId());
            articleService.insertBlog(article);
            return RestfulBuilder.successNoData(ResultCode.SUCCESS);
        }
        return RestfulBuilder.faile(ResultCode.USER_NOT_LOGGED_IN);
    }

    @PutMapping("articleSave")
    @ResponseBody
    public Restful articleUpdate(@RequestBody BlogArticle article, HttpSession session) {
        BlogAccount account = (BlogAccount) session.getAttribute("account");
        if (account != null){
            article.setAccountId(account.getAccountId());
            articleService.updateBlog(article);
            return RestfulBuilder.successNoData(ResultCode.SUCCESS);
        }
        return RestfulBuilder.faile(ResultCode.USER_NOT_LOGGED_IN);
    }

    /**
     * 修改博客状态 激活
     * @param id 未激活博客id
     * @return 转发到我的博客页面
     */
    @PostMapping("state")
    public String articleState(Integer id) {
        articleService.updateState(id);
        return "blog/myBlog";
    }

}
